package com.itic.system.pay.userPay.service;

import com.itic.appbase.applications.redis.IRedisDao;
import com.itic.appbase.framework.type.CollageSuccess;
import com.itic.appbase.framework.type.OrderState;
import com.itic.appbase.framework.type.RedisKey;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.system.collage.collage.persistence.model.Collage;
import com.itic.system.collage.collage.service.CollageService;
import com.itic.system.order.order.persistence.model.Order;
import com.itic.system.order.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.pay.userPay.persistence.dao.PayUserLogDao;
import com.itic.system.pay.userPay.persistence.model.PayUserLog;
import com.itic.system.pay.userPay.persistence.model.PayUserLogExample;

import java.text.ParseException;
import java.util.Date;


@Service
@Transactional
public class PayUserLogService
     extends CrudService<PayUserLogDao,PayUserLog,PayUserLogExample> {
    @Autowired
    private PayUserLogDao payUserLogDao;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CollageService collageService;
    @Autowired
    private IRedisDao<String,Object> redisDao;


    /**
     *  支付成功后处理名单业务
     * @param appid 商家appId
     * @param out_trade_no 订单号
     * @param finishTime 支付完成时间
     * @param total_fee 实付款
     * @param transaction_id  微信支付订单号
     * @param openid 用户openId
     * @param mch_id 商户号
     */
    public void  paySuccessDealOrder(String appid,String  out_trade_no,String  finishTime,String  total_fee,String  transaction_id, String  openid, String mch_id) throws ParseException {
        Order order = this.orderService.getOrderByOrderNo(out_trade_no);
        if(order!=null){
            OrderState orderState = OrderState.getOrderState(order.getState());
            if(OrderState.Paying.getState().equals(orderState.getState())){
                //支付成功修改订单状态
                String result = this.orderService.editOrderState(order,OrderState.Sending);
                if(SUCCESS.equals(result)){
                    //添加支付记录
                    Date finishDate = DateHelper.parseDate(finishTime,"yyyy-MM-dd HH:mm:ss");
                    this.addPayUserLog(order.getBuyUserId(),appid,order.getOrderNo(),finishDate,Double.parseDouble(total_fee),transaction_id,openid,mch_id,order.getCollageId(),order.getSellerUserId());
                    Collage collage = this.collageService.get(order.getCollageId());
                    int minut = collage.getCollageLimitTime() ==null?1:collage.getCollageLimitTime();
                    //添加拼团最大人数到redis,方便加锁 (失效时间为拼团结束时间)
                    StringBuffer key = new StringBuffer(RedisKey.CollageBuyUser.getKey()).append(collage.getId()).append(order.getBuyUserId());
                    this.redisDao.set(key.toString(),key.toString(),minut*60);

                }
            }
        }
    }

    /**
     * 添加用户支付记录
     * @param userId
     * @param appId
     * @param orderNo
     * @param finishTime
     * @param actualPay
     * @param transactionId
     * @param openId
     * @param mchId
     * @param collageId
     * @param reciveUserId
     * @return
     */
    public String addPayUserLog(String userId,String appId, String  orderNo, Date finishTime, double  actualPay, String  transactionId, String  openId, String mchId,String collageId,String reciveUserId){
        PayUserLog payUserLog = new PayUserLog();
        payUserLog.setUserId(userId);
        payUserLog.setAppId(appId);
        payUserLog.setOrderNo(orderNo);
        payUserLog.setFinishTime(finishTime);
        payUserLog.setActualPay(actualPay);
        payUserLog.setTransactionId(transactionId);
        payUserLog.setOpenId(openId);
        payUserLog.setMchId(mchId);
        payUserLog.setCollageId(collageId);
        payUserLog.setReciveUserId(reciveUserId);
        return this.add(payUserLog);
    }

    /**
     * 根据订单号查询支付信息
     * @param orderNo 订单号
     * @return
     */
    public PayUserLog getPayUserLogByOrderNo(String orderNo){
        PayUserLog payUserLog = this.payUserLogDao.getPayUserLogByOrderNo(orderNo);
        return payUserLog;
    }


}